package com.xiaoma.lock.condition;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadTest2 {

  private static Lock lock = new ReentrantLock();
  private static Condition subCondition = lock.newCondition();
  private static boolean subFlag = false;

  public static void main(String[] args) {
    ExecutorService threadPool = Executors.newFixedThreadPool(3);
    threadPool.execute(new Runnable() {
      public void run() {
        for (int i = 0; i < 5; i++) {
          lock.lock();
          try {
            if (subFlag) {
              subCondition.await();
            }
            for (int j = 0; j < 2; j++) {
              System.out.println(Thread.currentThread().getName()
                  + ":i=" + i + ",j=" + j);
            }
            subFlag = true;
            subCondition.signal();
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            lock.unlock();
          }
        }
      }

    });
    threadPool.shutdown();


    for (int i = 0; i < 5; i++) {
      lock.lock();
      try {
        if (!subFlag) {
          subCondition.await();
        }
        for (int j = 0; j < 4; j++) {
          System.out.println(Thread.currentThread().getName() + ":i="
              + i + ",j=" + j);
        }
        subFlag = false;
        subCondition.signal();
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        lock.unlock();
      }
    }


  }
}
